/**************************************************************
 *@ProjectName: blweibo
 *@FileName: BaseActivity.java
 *@Date: 2011-5-31
 *@Copyright: 2011 www.hrbanlv.com Inc. All rights reserved.
 **************************************************************/
package com.zhiweibo;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AbsListView;
import android.widget.AbsListView.LayoutParams;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.zhiweibo.models.UnreadNum;

/**
 * The Class BaseActivity.
 * 
 * @author SunHaibin
 * @since Android 1.6
 * @history 2011-5-31
 */
public abstract class BaseActivity extends Activity implements OnClickListener {

    /** The ly. */
    protected BaseLayout ly;

    /** The m loadmore item. */
    public LinearLayout mLoadmoreItem;

    /** The m reload item. */
    public LinearLayout mReloadItem;

    /** The m progress bar. */
    public ProgressBar mProgressBar;

    /** The handle. */
    public static Handler handle;

    /**
     * Instantiates a new base activity.
     */
    public BaseActivity() {
        handle = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                boolean b2 = SettingsPref.isNotifyComment(BaseActivity.this);
                boolean b3 = SettingsPref.isNotifyAt(BaseActivity.this);
                boolean b4 = SettingsPref.isNotifyMessage(BaseActivity.this);

                if (b2 && UnreadNum.atmsg > 0) {
                    ly.mButton1.setText(getString(R.string.main_at) + "(" + UnreadNum.atmsg + ")");
                } else {
                    ly.mButton1.setText(R.string.main_at);
                }
                if (b3 && UnreadNum.comment > 0) {
                    ly.mButton2.setText(getString(R.string.main_comment) + "(" + UnreadNum.comment + ")");
                } else {
                    ly.mButton2.setText(R.string.main_comment);
                }
                if (b4 && UnreadNum.message > 0) {
                    ly.mButton3.setText(getString(R.string.main_message) + "(" + UnreadNum.message + ")");
                } else {
                    ly.mButton3.setText(R.string.main_message);
                }
            }
        };
    }

    /**
     * Sets the title bar.
     * 
     * @param paramInt
     *            the param int
     * @param paramString1
     *            the param string1
     * @param paramString2
     *            the param string2
     * @param paramString3
     *            the param string3
     */
    protected void setTitleBar(int paramInt, String paramString1, String paramString2, String paramString3) {
        if (ly != null) {
            ly.setButtonTypeAndInfo(paramInt, paramString1, paramString2, paramString3);
        }
    }

    /**
     * Sets the view.
     * 
     * @param paramInt
     *            the new view
     */
    protected void setView(int paramInt) {
        ly = new BaseLayout(this, paramInt);
        setContentView(ly);
        ly.leftButton.setOnClickListener(this);
        ly.rightButton.setOnClickListener(this);
        ly.mButton1.setOnClickListener(this);
        ly.mButton2.setOnClickListener(this);
        ly.mButton3.setOnClickListener(this);
    }

    /*
     * (non-Javadoc)
     * 
     * @see android.app.Activity#onCreate(android.os.Bundle)
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mLoadmoreItem = new LinearLayout(this);
        Drawable localDrawable2 = getResources().getDrawable(R.drawable.list_item_bg);
        mLoadmoreItem.setBackgroundDrawable(localDrawable2);
        mLoadmoreItem.setOrientation(LinearLayout.HORIZONTAL);
        mLoadmoreItem.setGravity(17);
        mLoadmoreItem.removeAllViews();
        mLoadmoreItem.addView(setTextView(R.string.more));

        Drawable localDrawable3 = getResources().getDrawable(R.drawable.list_item_bg);
        mReloadItem = new LinearLayout(this);
        mReloadItem.setBackgroundDrawable(localDrawable3);
        mReloadItem.setOrientation(LinearLayout.HORIZONTAL);
        mReloadItem.setGravity(17);
        mReloadItem.removeAllViews();
        mReloadItem.addView(setTextView(R.string.main_reload));

        mProgressBar = new ProgressBar(this, null, 16843400);

    }

    /**
     * Sets the text view.
     * 
     * @param id
     *            the id
     * @return the view
     */
    public View setTextView(int id) {
        TextView localTextView = new TextView(this);
        (localTextView).setText(id);
        (localTextView).setGravity(17);
        int i = -2;
        int j = getResources().getDimensionPixelSize(R.dimen.sta_height);
        AbsListView.LayoutParams localLayoutParams = new LayoutParams(i, j);
        localTextView.setLayoutParams(localLayoutParams);
        localTextView.setTextColor(Color.BLACK);
        return localTextView;
    }

    /*
     * (non-Javadoc)
     * 
     * @see android.view.View.OnClickListener#onClick(android.view.View)
     */
    @Override
    public void onClick(View paramView) {
        if (paramView == ly.leftButton) {
            handleTitleBarEvent(1);
        } else if (paramView == ly.rightButton) {
            handleTitleBarEvent(0);
        } else if (paramView == ly.mButton1) {
            handleTitleBarEvent(2);
        } else if (paramView == ly.mButton2) {
            handleTitleBarEvent(3);
        } else if (paramView == ly.mButton3) {
            handleTitleBarEvent(4);
        }

    }

    /**
     * Handle title bar event.
     * 
     * @param paramInt
     *            the param int
     */
    protected abstract void handleTitleBarEvent(int paramInt);

    /**
     * Reload begine.
     * 
     * @param paramInt
     *            the param int
     */
    protected void reloadBegine(int paramInt) {
        if (getClass().equals(HomeListActivity.class)) {
            ly.progressButton.setVisibility(View.VISIBLE);
            ly.rightButton.setVisibility(View.GONE);
            ly.leftButton.setEnabled(false);
        }
        switch (paramInt) {
        case 0:
            mLoadmoreItem.removeAllViews();
            mLoadmoreItem.addView(setTextView(R.string.more));
            mLoadmoreItem.addView(mProgressBar);
            break;
        case 1:
            mReloadItem.removeAllViews();
            mReloadItem.addView(setTextView(R.string.loading));
            mReloadItem.addView(mProgressBar);
            break;
        }
    }

    /**
     * Reload end.
     */
    protected void reloadEnd() {
        if (getClass().equals(HomeListActivity.class)) {
            ly.progressButton.setVisibility(View.GONE);
            ly.rightButton.setVisibility(View.VISIBLE);
            ly.leftButton.setEnabled(true);
        }
        mLoadmoreItem.removeAllViews();
        mLoadmoreItem.addView(setTextView(R.string.more));
        mReloadItem.removeAllViews();
        mReloadItem.addView(setTextView(R.string.main_reload));
    }
}
